package com.zgw.his.design.resource;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;

import com.zgw.his.server.emr.IEmrService;
import com.zgw.his.server.emr.entity.TbaseInputSelected;
import com.zgw.his.server.emr.entity.TbaseInputSelectedItem;
import com.zgw.his.server.emr.entity.Telement;
import com.zgw.his.server.exceptions.BusinessException;
import com.zgw.his.server.util.SpringBeanFactory;

/**
 * 其础数据源
 * */
public class BaseResource {
	private static Map<String, Telement> elementMap = new HashMap<String, Telement>();
	private static Map<String, TbaseInputSelected> inputMap = new HashMap<String, TbaseInputSelected>();
	private static IEmrService service = SpringBeanFactory
			.getServiceBean(IEmrService.class);

	public static void loadElement() {
		try {
			elementMap.clear();
			List<Telement> list = service.getAllEntity(Telement.class);
			if (list != null) {
				for (Telement e : list) {
					elementMap.put(e.getFcode(), e);
				}
			}
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}

	public static void loadInput() {
		inputMap.clear();
		try {
			List<TbaseInputSelected> list = service
					.getAllEntity(TbaseInputSelected.class);
			if (list != null) {
				for (TbaseInputSelected t : list) {
					inputMap.put(t.getFid().toString(), t);
				}
			}
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}

	public static Collection<Telement> getElements() {
		return elementMap.values();
	}

	public static String getElementName(String code) {
		if (elementMap.containsKey(code)) {
			return elementMap.get(code).getFname();
		}
		return null;
	}

	public static Collection<TbaseInputSelected> getBaseInputSelected() {
		return inputMap.values();
	}

	public static TbaseInputSelected getInputSelected(String fid) {
		if (fid != null && inputMap.containsKey(fid)) {
			return inputMap.get(fid);
		}
		return null;
	}

	public static String getInputSelectedName(String fid) {
		if (fid != null && inputMap.containsKey(fid)) {
			return inputMap.get(fid).getFname();
		}
		return null;
	}

	public static List<TbaseInputSelectedItem> getBaseInputSelectedItems(
			Long fid) {
		Assert.isNotNull(fid);
		try {
			String hql = "from TbaseInputSelectedItem t where t.id.fbaseSelectedId="
					+ fid + "";
			return service.find(hql);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
}
